<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore, UserRole } from '@/stores/userStore'
import { ElMessage, ElMessageBox } from 'element-plus'
import { UserFilled } from '@element-plus/icons-vue'

// 获取用户状态管理
const userStore = useUserStore()
const router = useRouter()

// 侧边栏折叠状态
const isCollapse = ref(false)

// 计算用户角色显示文本
const roleText = computed(() => {
  if (!userStore.userInfo) return ''
  return userStore.userInfo.role === UserRole.ADMIN ? '管理员' : '普通用户'
})

/**
 * 处理退出登录
 */
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      userStore.logout()
      ElMessage.success('已退出登录')
      router.push('/login')
    })
    .catch(() => {
      // 取消退出
    })
}

/**
 * 切换侧边栏折叠状态
 */
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<template>
  <el-container class="layout-container">
    <!-- 顶部栏 -->
    <el-header class="layout-header">
      <div class="header-left">
        <el-button type="primary" :icon="isCollapse ? 'Expand' : 'Fold'" circle @click="toggleSidebar" />
        <h2 class="system-title">巴中市滑坡风险预警系统</h2>
      </div>
      <div class="header-right">
        <el-dropdown trigger="click">
          <div class="user-info">
            <el-avatar :icon="UserFilled" />
            <span class="username">{{ userStore.userInfo?.username }}</span>
            <span class="role-tag">{{ roleText }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleLogout">
                <el-icon>
                  <SwitchButton />
                </el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 侧边栏 -->
      <el-aside class="layout-aside" width="auto">
        <el-menu :collapse="isCollapse" :default-active="$route.path" class="layout-menu" router unique-opened>
          <!-- 管理员菜单项 -->
          <template v-if="userStore.userInfo?.role === UserRole.ADMIN">
            <el-menu-item index="/admin/welcome">
              <el-icon>
                <HomeFilled />
              </el-icon>
              <template #title>管理员首页</template>
            </el-menu-item>
            <el-sub-menu index="system">
              <template #title>
                <el-icon>
                  <Setting />
                </el-icon>
                <span>系统管理</span>
              </template>
              <el-menu-item index="/admin/users">
                <el-icon>
                  <User />
                </el-icon>
                <span>用户管理</span>
              </el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="data">
              <template #title>
                <el-icon>
                  <DataAnalysis />
                </el-icon>
                <span>数据管理</span>
              </template>
              <el-menu-item index="/admin/landslide">
                <el-icon>
                  <Warning />
                </el-icon>
                <span>滑坡要素管理</span>
              </el-menu-item>
            </el-sub-menu>
          </template>

          <!-- 普通用户菜单项 -->
          <template v-else>
            <el-menu-item index="/user/welcome">
              <el-icon>
                <HomeFilled />
              </el-icon>
              <template #title>用户首页</template>
            </el-menu-item>
          </template>

          <!-- 公共菜单项 -->
          <el-sub-menu index="map">
            <template #title>
              <el-icon>
                <Location />
              </el-icon>
              <span>地图功能</span>
            </template>
            <el-menu-item index="/map/view">
              <el-icon>
                <MapLocation />
              </el-icon>
              <span>地图查看</span>
            </el-menu-item>
          </el-sub-menu>

          <!-- 统计分析菜单项 -->
          <el-menu-item index="/statistics">
            <el-icon>
              <DataAnalysis />
            </el-icon>
            <span>统计分析</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区域 -->
      <el-main class="layout-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  width: 100%;
}

.layout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(90deg, #ffecd2 0%, #fcb69f 80%, #f85032 100%);
  box-shadow: 0 2px 8px rgba(255, 87, 34, 0.08);
  padding: 0 20px;
  height: 60px;
  z-index: 999;

  .header-left {
    display: flex;
    align-items: center;
    gap: 15px;

    .system-title {
      margin: 0;
      font-size: 18px;
      color: #b71c1c;
    }
  }

  .header-right {
    display: flex;
    align-items: center;

    .user-info {
      display: flex;
      align-items: center;
      cursor: pointer;
      padding: 5px 10px;
      border-radius: 4px;
      transition: background-color 0.3s;

      &:hover {
        background-color: #ffe0b2;
      }

      .username {
        margin: 0 10px;
        font-size: 14px;
      }

      .role-tag {
        background-color: #fff3e0;
        color: #f44336;
      }
    }
  }
}

.main-container {
  height: calc(100vh - 60px);
}

.layout-aside {
  background: linear-gradient(180deg, #fff3e0 0%, #ffe0b2 100%);
  border-right: 1px solid #fcb69f;
}

.layout-menu {
  background: transparent;
}

.layout-main {
  background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
  padding: 20px;
  overflow-y: auto;
}
</style>
